//
//  LeveyPopListView.m
//  LeveyPopListViewDemo
//
//  Created by Levey on 2/21/12.
//  Copyright (c) 2012 Levey. All rights reserved.
//

#import "LeveyPopListView.h"
#import "Utility.h"
#import "Config.h"
#import "LeveyPopListViewCell.h"



@interface LeveyPopListView (private)
- (void)fadeIn;
@end

@implementation LeveyPopListView
@synthesize delegate;
#pragma mark - initialization & cleaning up
- (id)initWithOptions:(NSArray *)aOptions
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    if (self = [super initWithFrame:rect])
    {
        self.backgroundColor = [UIColor clearColor];
        UIView *tablebakgroundimageView=[[UIView alloc] initWithFrame:CGRectMake(216, -5, 100, 110)];
        tablebakgroundimageView.backgroundColor=[UIColor colorWithPatternImage:[Utility imageNamed:@"popMenu.png"]];
        [self addSubview:tablebakgroundimageView];
        self.options =aOptions;
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,2,97,128)];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled=NO;
        // self.tableView.showsVerticalScrollIndicator=NO;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
       // _tableView.separatorColor=[UIColor redColor];
        [tablebakgroundimageView addSubview:self.tableView];

    }
    return self;    
}

- (void)dealloc
{
}

#pragma mark - Private Methods
- (void)fadeIn{
    /*
     
     self.transform = CGAffineTransformMakeScale(1.3, 1.3);
     self.alpha = 0;
     [UIView animateWithDuration:.35 animations:^{
     self.alpha = 1;
     self.transform = CGAffineTransformMakeScale(1, 1);
     }];

     */
    CGRect frame=self.frame;
    frame.origin.y=-frame.size.height+64;
    self.frame=frame;
    [UIView animateWithDuration:0.5
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         CGRect frameEnd=self.frame;
                         frameEnd.origin.y=0;
                         self.frame=frameEnd;
                     }
                     completion:^(BOOL finished){
   
                     }];
}
- (void)fadeOut{
    /*
     [UIView animateWithDuration:.35 animations:^{
     self.transform = CGAffineTransformMakeScale(1.3, 1.3);
     self.alpha = 0.0;
     } completion:^(BOOL finished) {
     if (finished) {
     [self removeFromSuperview];
     }
     }];
     */
    __weak id weakSelf=self;
    [UIView animateWithDuration:0.3
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGRect frame=self.frame;
                         frame.origin.y=-frame.size.height;
                         self.frame=frame;
                     }
                     completion:^(BOOL finished){
                         __strong id strongSelf=weakSelf;
                        [strongSelf removeFromSuperview];
                     }];
}

#pragma mark - Instance Methods
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
    [aView addSubview:self];
    if (animated) {
        [self fadeIn];
    }
}

#pragma mark - Tableview datasource & delegates
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.options count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 32;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentity = @"PopListViewCell";
    
    LeveyPopListViewCell *cell = (LeveyPopListViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
    if (cell ==  nil) {
        cell = [[LeveyPopListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
    }
    int row = [indexPath row];
    cell.content.text=[self.options objectAtIndex:row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(leveyRightPopListView:didSelectedIndex:)]) {
        [self.delegate leveyRightPopListView:self didSelectedIndex:[indexPath row]];
    }
    
    // dismiss self
    [self fadeOut];
}
#pragma mark - TouchTouchTouch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    // tell the delegate the cancellation
    if (self.delegate && [self.delegate respondsToSelector:@selector(leveyPopListViewDidCancel)]) {
        [self.delegate leveyRightPopListViewDidCancel];
    }
    
    // dismiss self
    [self fadeOut];
}



@end
